//
//  UIViewController+GQHCategory.m
//  Xanthium
//
//  Created by GuanQinghao on 11/04/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "UIViewController+GQHCategory.h"
#import "NSString+GQHPredicate.h"


@implementation UIViewController (GQHCategory)

/// 提示视图 (确定)/(自定义|取消)
- (void)qh_alertWithTitle:(NSString *_Nullable)title message:(NSString *_Nullable)message actionTitle:(NSString *_Nullable)actionTitle handler:(void (^ __nullable)(UIAlertAction * _Nullable action))handler completion:(void (^ __nullable)(void))completion {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message: message preferredStyle:UIAlertControllerStyleAlert];
    
    if (actionTitle) {
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        
        [alertController addAction:[UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:handler]];
    } else {
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:handler]];
    }
    
    [self presentViewController:alertController animated:YES completion:completion];
}

/// 带输入框的警告弹窗
- (void)qh_alertWithTitle:(NSString *_Nullable)title message:(NSString *_Nullable)message inputHandler:(void (^ __nullable)(UITextField * _Nullable textField))inputHandler actionHandler:(void (^ __nullable)(UIAlertAction * _Nullable action, UIAlertController * _Nonnull alertController))actionHandler completion:(void (^ __nullable)(void))completion {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message: message preferredStyle:UIAlertControllerStyleAlert];
    
    [alertController addTextFieldWithConfigurationHandler:inputHandler];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        actionHandler(action,alertController);
    }]];
    
    [self presentViewController:alertController animated:YES completion:completion];
}

/// 拨打电话(弹出框提示)
- (void)qh_dialWithMobile:(NSString * _Nonnull)mobile {
    
    /// telprompt 可以上架
    NSString *dialString = [NSString stringWithFormat:@"telprompt:%@",mobile];
    
    if ([[UIDevice currentDevice].model isEqualToString:@"iPhone"]) {
        NSLog(@"%@",[UIDevice currentDevice].model);
        if (@available(iOS 10.0, *)) {
            /// iOS10后可用
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:dialString] options:@{ } completionHandler:^(BOOL success) {
                
                /// 拨打电话完成后处理
                
            }];
        } else {
            
            /// iOS10前可用
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:dialString]];
        }
    } else {
        
        /// 不支持拨打电话
        [self showToastWithText:@"当前设备不支持拨打电话"];
    }
}

@end




